package com.drug.service.impl;

import com.drug.domain.Result;
import com.drug.domain.SupervisoryBody;
import com.drug.mapper.SupervisoryBodyMapper;
import com.drug.service.SupervisoryBodyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SupervisoryBodyServiceImpl implements SupervisoryBodyService {
    @Autowired
    private SupervisoryBodyMapper supervisoryBodyMapper;

    //多条件查询
    @Override
    public List<SupervisoryBody> selectSupervisoryBody(SupervisoryBody supervisoryBody) {
        //监督机构名称
        if (supervisoryBody.getSbName()!=null&&supervisoryBody.getSbName()!=""){
            String name = supervisoryBody.getSbName();
            supervisoryBody.setSbName(name.toString());
        }else {
            supervisoryBody.setSbName(null);
        }
        //联系人
        if (supervisoryBody.getSbContacts()!=null&&supervisoryBody.getSbContacts()!=""){
            String sbContacts = supervisoryBody.getSbContacts();
            supervisoryBody.setSbContacts(sbContacts);
        }else {
            supervisoryBody.setSbContacts(null);
        }
        //email
        if (supervisoryBody.getSbEmail()!=null&&supervisoryBody.getSbEmail()!=""){
            String sbEmail = supervisoryBody.getSbEmail();
            supervisoryBody.setSbEmail(sbEmail);
        }else {
            supervisoryBody.setSbEmail(null);
        }
        return supervisoryBodyMapper.selectSupervisoryBody(supervisoryBody);
    }

    //分页查询
    @Override
    public Result pageSupervisoryFindAll(Integer page) {
        //使用分页插件
        PageHelper.startPage(page,5);
        Result result = new Result<>();
        List<SupervisoryBody> supervisoryBodyList = supervisoryBodyMapper.pageSupervisoryFindAll();
        PageInfo<SupervisoryBody> pageInfo = new PageInfo<>(supervisoryBodyList);
        result.setData(pageInfo);
        return result;
    }

    //逻辑删除
    @Override
    public void deleteSupervisoryBody(Integer sbId) {
        supervisoryBodyMapper.deleteSupervisoryBody(sbId);
    }

    //添加监督机构
    @Override
    public void insertSupervisoryBody(SupervisoryBody supervisoryBody) {
         supervisoryBodyMapper.insertSupervisoryBody(supervisoryBody);
    }

    //修改删除机构
    @Override
    public List<SupervisoryBody> updateSupervisoryBody(SupervisoryBody supervisoryBody) {
        supervisoryBodyMapper.updateSupervisoryBody(supervisoryBody);
        return null;
    }
    //查询监督机名
    @Override
    public Result selectSupervisoryname(String sbName) {
        Result result = new Result();
        List<SupervisoryBody> supervisory = supervisoryBodyMapper.selectSupervisoryname(sbName);
        for (int i = 0; i < supervisory.size(); i++) {
            if (sbName.equals(sbName)){
                result.setMsg("供货商已存在");
                result.setCode(0);
                break;
            }else {
                result.setMsg("供货商可用");
                result.setCode(1);
            }
        }
        return result;
    }

}
